---
id: models-with-same-name
title: 'Models with same name'
---

<!--MDX Import section-->

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<!--END MDX Import section-->

This Guide shows all the possibilities for a model to use different names.

:::note
If Classes with the same name are wanted, these methods need to be used so that Typegoose can differentiate between them
:::

:::info
Since `7.6.0` the name can also be changed in `getModelForClass` or `buildSchema` calls.
:::

## SchemaOptions collection

For this to work, `{ options: { automaticName } }` must be true.

`{ schemaOptions: { collection } }` can be used to set a custom collection a model should use.
Using `{ schemaOptions: { collection } }` with `automaticName` automatically adds a suffix of the collection to the model's name.

Example without `automaticName`:

```ts
@modelOptions({ schemaOptions: { collection: 'Something' } })
class MultiModel {}

const model = getModelForClass(MultiModel);
expect(model.modelName).to.be.equal('MultiModel');
```

Example with `automaticName`:

```ts
@modelOptions({ schemaOptions: { collection: 'Something' }, options: { automaticName: true } })
class MultiModel {}

const model = getModelForClass(MultiModel);
expect(model.modelName).to.be.equal('MultiModel_Something');
```

## Typegoose Custom Options "customName"

`{ options: { customName } }` can be used to set a custom model name.

Example:

```ts
@modelOptions({ options: { customName: 'CustomName' } })
class CustomNameOption {}

const model = getModelForClass(CustomNameOption);
expect(model.modelName).to.be.equal('CustomName');
```

If `{ options: { customName } }` is used with `{ options: { automaticName: true } }`, then it will be used as a *suffix* of the normal name.

Example:

```ts
@modelOptions({ options: { customName: 'CustomName', automaticName: true } })
class CustomNameOption {}

const model = getModelForClass(CustomNameOption);
expect(model.modelName).to.be.equal('CustomNameOption_CustomName');
```

## Disable Caching

Since Typegoose `10.2.0` there is also the option of disabling the cache globally with [`setGlobalOptions`](../../api/functions/setGlobalOptions.md)'s [`disableGlobalCaching`](../../api/functions/setGlobalOptions.md#disableglobalcaching) or locally via [`@modelOptions`](../../api/decorators/modelOptions.md#disablecaching).

<Tabs groupId="caching-global-local">
  <TabItem value="global" label="Disable Cache globally">

```ts
import { setGlobalOptions, getModelForClass, prop } from "@typegoose/typegoose";

setGlobalOptions({ globalOptions: { disableGlobalCaching: true } });

class Kitten {
  @prop()
  public name: string;
}

const KittenModelDefault = getModelForClass(Kitten);
const KittenModelCon1 = getModelForClass(Kitten, { existingConnection: mongoose.createConnection() });

// OR
{
  class Kitten {
    @prop()
    public name: string;
  }

  const KittenModel = getModelForClass(Kitten);
  assert.ok(!!KittenModel.schema.path('name'));
}
{
  class Kitten {
    @prop()
    public nameTag: string;
  }

  const KittenModel = getModelForClass(Kitten, { existingConnection: mongoose.createConnection() }); // still requires being defined on a different connection / mongoose instance
  assert.ok(!!KittenModel.schema.path('nameTag'));
}
```

  </TabItem>
  <TabItem value="local" label="Disable Cache locally" default>

```ts
import { modelOptions, getModelForClass, prop } from "@typegoose/typegoose";

@modelOptions({ options: { disableCache: true } })
class Kitten {
  @prop()
  public name: string;
}

const KittenModelDefault = getModelForClass(Kitten);
const KittenModelCon1 = getModelForClass(Kitten, { existingConnection: mongoose.createConnection() });

// OR
{
  class Kitten {
    @prop()
    public name: string;
  }

  const KittenModel = getModelForClass(Kitten, { options: { disableCaching: true } });
  assert.ok(!!KittenModel.schema.path('name'));
}
{
  class Kitten {
    @prop()
    public nameTag: string;
  }

  const KittenModel = getModelForClass(Kitten, { existingConnection: mongoose.createConnection(), options: { disableCaching: true } }); // still requires being defined on a different connection / mongoose instance
  assert.ok(!!KittenModel.schema.path('nameTag'));
}
```

  </TabItem>
</Tabs>

:::note
Models still cannot be defined more than once in the same connection / mongoose instance.
:::
:::caution
Setting the Cache to be disabled globally will make some functions that rely on it error, see [`E033`](../error-warning-details.md#cache-disabled-e033) and [`disableGlobalCaching`](../../api/functions/setGlobalOptions.md#disableglobalcaching) which effects it will have.
:::

## Notes

For more details on the usage of these naming features, please look into [the tests that are written for them.](https://github.com/typegoose/typegoose/blob/r6/master/test/tests/customName.test.ts)

See also [Typegoose's Name Generation](./name-generation.md) for a complete (and hopefully simple) way to understand name generation.
